package com.jsyl.lwbk.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.jsyl.lwbk.business.constant.IntegrationConstant;
import com.jsyl.lwbk.business.domain.ClassPlanDetail;
import com.jsyl.lwbk.business.domain.Integration;
import com.jsyl.lwbk.business.dto.ClassPlanDetailDTO;
import com.jsyl.lwbk.business.dto.SaveProjectDTO;
import com.jsyl.lwbk.business.dto.TrainPackageDTO;
import com.jsyl.lwbk.business.mapper.ClassPlanDetailMapper;
import com.jsyl.lwbk.business.mapper.TrainPackageMapper;
import com.jsyl.lwbk.business.mapper.IntegrationMapper;
import com.jsyl.lwbk.business.service.ClassPlanDetailService;
import com.jsyl.lwbk.common.core.domain.Ztree;
import com.jsyl.lwbk.common.utils.DateUtils;
import com.jsyl.lwbk.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fitch
 * 2022/2/21
 */
@Service
public class ClassPlanDetailServiceImpl implements ClassPlanDetailService {

    @Autowired
    private ClassPlanDetailMapper classPlanDetailMapper;
    @Autowired
    private IntegrationMapper integrationMapper;
    @Autowired
    private TrainPackageMapper trainPackageMapper;

    @Override
    public List<ClassPlanDetail> findList(Long classPlanId, Integer classTimes) {
        List<ClassPlanDetail> list = this.classPlanDetailMapper.findList(classPlanId, classTimes);
        if (!CollectionUtils.isEmpty(list)) {
            //项目编号转成名称
            Map<String, String> codeName = getCodeName();
            Map<String, String> planName = getPlanName();
            for (ClassPlanDetail c : list) {
                if (c.getProjectCode() != null) {
                    String name = c.getProjectCode();
                    String[] projectCodes = c.getProjectCode().split(",");
                    for (String s : projectCodes) {
                        name = name.replace(s, codeName.get(s));
                    }
                    c.setProjectName(name);
                }
                if (!StringUtils.isEmpty(c.getTrainPackageId())) {
                    String name = c.getTrainPackageId();
                    String[] trainPackages = c.getTrainPackageId().split(",");
                    for (String s : trainPackages) {
                        name = name.replace(s, planName.get(s));
                    }
                    c.setTrainName(name);
                }

            }
        }else {
            for(int i=1;i<= IntegrationConstant.CLASS_LINK_COUNT;i++){
                ClassPlanDetail c = new ClassPlanDetail();
                c.setId(String.valueOf(IdWorker.getId()));
                c.setClassPlanId(classPlanId.toString());
                c.setClassTimes(classTimes);
                c.setLinkNo(i);
                c.setLinkRuleType(0);
                c.setStatus(0);
                c.setCreateTime(DateUtils.getNowDate());
                this.classPlanDetailMapper.insert(c);
                list.add(c);
            }
        }
        return list;
    }

    private Map<String, String> getCodeName() {
        List<Integration> list = integrationMapper.findList(null);
        Map<String, String> result = new HashMap<>();
        for (Integration i : list) {
            result.put(i.getCode(), i.getName());
        }
        return result;
    }

    private Map<String, String> getPlanName() {
        List<TrainPackageDTO> list = trainPackageMapper.findList(null,null);
        Map<String, String> result = new HashMap<>();
        for (TrainPackageDTO c : list) {
            result.put(c.getId(), c.getTrainName());
        }
        return result;
    }

    @Override
    public void updateLinkRuleType(Long id, Integer linkRuleType) {
        this.classPlanDetailMapper.updateLinkRuleType(id, linkRuleType);
    }


    @Override
    public List<Ztree> getProjectTree(Long classPlanDetailId) {
        List<Integer> leafType = new ArrayList<>();
        leafType.add(1);
        leafType.add(2);
        leafType.add(4);
        List<Integration> integrationList = this.integrationMapper.findList(leafType);
        if (integrationList == null) {
            return new ArrayList<Ztree>();
        }
        List<String> checkedList = new ArrayList<>();
        ClassPlanDetail classPlanDetail = this.classPlanDetailMapper.findById(classPlanDetailId);
        if (classPlanDetail != null && classPlanDetail.getProjectCode() != null) {
            checkedList = CollectionUtils.arrayToList(classPlanDetail.getProjectCode().split(","));
        }
        return initZtree(integrationList, checkedList, false);
    }

    public List<Ztree> initZtree(List<Integration> menuList, List<String> checkedList, boolean permsFlag) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = CollectionUtils.isEmpty(checkedList);
        for (Integration menu : menuList) {
            Ztree ztree = new Ztree();
            ztree.setId(menu.getId());
            ztree.setpId(menu.getParentId());
            ztree.setName(menu.getName());
            ztree.setTitle(menu.getName());
            if (!isCheck) {
                ztree.setChecked(checkedList.contains(menu.getCode()));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public int saveProject(SaveProjectDTO dto) {
        ClassPlanDetail classPlanDetail = this.classPlanDetailMapper.findById(dto.getClassPlanDetailId());
        if (classPlanDetail == null) {
            return 0;
        }
        //自选环节，保存项目信息
        classPlanDetail.setProjectCode(null);
        if ((dto.getLinkRuleType().equals(1) || dto.getLinkRuleType().equals(3)) && dto.getIntegrationIds() != null) {
            String projectCode = this.integrationMapper.getCodeById(CollectionUtils.arrayToList(dto.getIntegrationIds().split(",")));
            classPlanDetail.setProjectCode(projectCode);
        }
        //训练包环节，保存训练包信息
        classPlanDetail.setTrainPackageId(null);
        if (dto.getLinkRuleType().equals(2) && dto.getTrainPackageId() != null) {
            classPlanDetail.setTrainPackageId(dto.getTrainPackageId());
        }
        classPlanDetail.setUpdateUser(dto.getUpdateUser());
        this.classPlanDetailMapper.update(classPlanDetail);
        return 1;
    }

    @Override
    public int sortProject(SaveProjectDTO dto) {
        ClassPlanDetail classPlanDetail = this.classPlanDetailMapper.findById(dto.getClassPlanDetailId());
        if (classPlanDetail == null) {
            return 0;
        }
        String projectCode = StringUtils.list2String(dto.getProjectCodes(), ",");
        //自选环节，保存项目信息
        if (projectCode.startsWith("S")) {
            classPlanDetail.setProjectCode(projectCode);
        }
        //训练包环节，保存训练包信息
        if (!projectCode.startsWith("S")) {
            classPlanDetail.setTrainPackageId(projectCode);
        }
        classPlanDetail.setUpdateUser(dto.getUpdateUser());
        this.classPlanDetailMapper.update(classPlanDetail);
        return 1;
    }

    @Override
    public List<ClassPlanDetailDTO> listThemeProject(Long classPlanId) {
        List<ClassPlanDetailDTO> list =this.classPlanDetailMapper.listThemeProject(classPlanId);
        int k=1;
        for(int i=0;i<list.size();i++){
            list.get(i).setDay(k);
            if(i>0&&i%2==1){
                k++;
            }
        }
        return list;
    }
}
